package com.juyu.miliao.common.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 通用工具类
 *
 * @author jh
 * 2018/7/18 14:34
 */
public class CommonUtil<T> {

    private CommonUtil() {
        throw new RuntimeException("工具类不允许创建实例");
    }

    /**
     * 判断String是否为空
     *
     * @param s String
     * @return boolean
     */
    public static boolean isEmptyString(String s) {
        return s == null || s.trim().length() == 0;
    }

    /**
     * 判断String是否为空
     *
     * @param s String
     * @return boolean
     */
    public static boolean isNotEmptyString(String s) {
        return s != null && s.trim().length() > 0;
    }

    /**
     * 判断collection是否为空或null
     *
     * @param collection collection
     * @return boolean
     */
    public static boolean isEmptyCollection(Collection collection) {
        return collection == null || collection.isEmpty();
    }

    /**
     * 判断collection是否为空或null
     *
     * @param collection collection
     * @return boolean
     */
    public static boolean isNotEmptyCollection(Collection collection) {
        return collection != null && !collection.isEmpty();
    }

    /**
     * 判断map是否为空或null
     *
     * @param map map
     * @return boolean
     */
    public static boolean isEmptyMap(Map map) {
        return map == null || map.isEmpty();
    }

    /**
     * 判断map是否为空或null
     *
     * @param map map
     * @return boolean
     */
    public static boolean isNotEmptyMap(Map map) {
        return map != null && !map.isEmpty();
    }

    /**
     * object转int
     *
     * @param s obj
     * @return int
     */
    public static int null2Int(Object s) {
        int v = 0;
        if (s != null) {
            try {
                v = Integer.parseInt(s.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return v;
    }

    /**
     * object转float
     *
     * @param s object
     * @return float
     */
    public static float null2Float(Object s) {
        float v = 0.0F;
        if (s != null) {
            try {
                v = Float.parseFloat(s.toString());
            } catch (Exception localException) {
                localException.printStackTrace();
            }
        }
        return v;
    }

    /**
     * object转double
     *
     * @param s object
     * @return double
     */
    public static double null2Double(Object s) {
        double v = 0.0D;
        if (s != null) {
            try {
                v = Double.parseDouble(null2String(s));
            } catch (Exception localException) {
                localException.printStackTrace();
            }
        }
        return v;
    }

    /**
     * object转boolean
     *
     * @param s object
     * @return boolean
     */
    public static boolean null2Boolean(Object s) {
        boolean v = false;
        if (s != null) {
            try {
                v = Boolean.parseBoolean(s.toString());
            } catch (Exception localException) {
                localException.printStackTrace();
            }
        }
        return v;
    }

    /**
     * object转String
     *
     * @param s object
     * @return String
     */
    public static String null2String(Object s) {
        return s == null ? "" : s.toString().trim();
    }

    /**
     * object转Long
     *
     * @param s object
     * @return Long
     */
    public static Long null2Long(Object s) {
        Long v = -1L;
        if (s != null) {

            try {
                v = Long.parseLong(s.toString());
            } catch (Exception localException) {
                localException.printStackTrace();
            }
        }
        return v;
    }

    /**
     * String转decimal
     *
     * @param s     String
     * @param scale 小数位数
     * @return BigDecimal
     */
    public static BigDecimal string2Decimal(String s, int scale) {
        if (!isEmptyString(s) && scale > 0) {
            try {
                BigDecimal bd = new BigDecimal(s.trim());
                return bd.setScale(scale, BigDecimal.ROUND_HALF_UP);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 切分 ids  1,2,3
     *
     * @param ids 1,2,3
     * @return List<long>
     */
    public static List<Long> changeIds(String ids) {
        List<Long> longList = new ArrayList<>();
        if (!isEmptyString(ids)) {
            String[] a = ids.split(",");
            for (String s : a) {
                Long v = null2Long(s);
                if (v != -1L) {
                    longList.add(v);
                }
            }
        }
        return longList;
    }
    /**
     * 切分 String  a,b,c
     *
     * @param str a,b,c
     * @return List<String>
     */
    public static List<String> changeString(String str) {
        List<String> strList = new ArrayList<>();
        if (!isEmptyString(str)) {
            String[] a = str.split(",");
            for (String s : a) {
                if (!s.equals("")) {
                    strList.add(s);
                }
            }
        }
        return strList;
    }




    /**
     * 清除List<Long>中的null元素
     *
     * @param list list
     */
    public static List<Long> cleanListLong(List<Long> list) {
        List<Long> listObj = new ArrayList<>();
        if (list != null && !list.isEmpty()) {
            for (Long obj : list) {
                if (obj != null) {
                    listObj.add(obj);
                }
            }
        }
        return listObj;
    }


}
